/**
 * 
 */
package com.cxc.myweb.demo.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cxc.myweb.demo.model.bpo.UserBpo;
import com.cxc.myweb.demo.model.dto.User;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author chenxiaocang
 * 
 */
public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 6729363294745168591L;
	
	final String RET="ret";
	final String CONTENT="CONTENT";
	
	final String USERID="userId";
	final String USERNAME="userName";
	final String USERPASSWORD="userPassword";
	
	private UserBpo userBpo;
	private Map<String,Object> dataMap;
	
	Logger logger= LoggerFactory.getLogger(UserAction.class);
	
	public UserAction() {
		dataMap = new HashMap<String,Object>();
	}
	
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String validateUser(){
		dataMap.clear();
		String userName = (String)ServletActionContext.getRequest().getParameter("userName");
		String userPassword = (String)ServletActionContext.getRequest().getAttribute("userPassword");
		int ret = userBpo.validateUser(userName,userPassword);
		dataMap.put(RET, ret);
		dataMap.put(CONTENT, userBpo.getMessage());
		return SUCCESS;		
	}
	
	public String queryUser(){
		dataMap.clear();
		String userId=(String)ServletActionContext.getRequest().getParameter(USERID);
		User user = userBpo.queryUserByPk(Integer.parseInt(userId));
		if ( user!=null){
			dataMap.put(RET, 1);
			dataMap.put(CONTENT, user);
		}
		else {
			dataMap.put(RET, 0);
			dataMap.put(CONTENT, "用户不存在.");
		}
		return SUCCESS;		
	}
	
	public String queryUserList() {
		dataMap.clear();
		List<User> userList = userBpo.queryUsers(null);
		dataMap.put(RET, userList.size());
		dataMap.put(CONTENT, userList);
		return SUCCESS;
	}
	
	public UserBpo getUserBpo() {
		return userBpo;
	}

	public void setUserBpo(UserBpo userBpo) {
		this.userBpo = userBpo;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}
	
}
